package com.tamartech.greendb.data;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

@Entity
@Table(name = "person")
public class Person
{
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) 
        {
            return false;
        }
        Person rhs = (Person) obj;

        boolean ret = new EqualsBuilder()
//                     .appendSuper(super.equals(obj))
                     .append(getFirstNames(), rhs.getFirstNames())
                     .append(getSurname(), rhs.getSurname())
                     .isEquals();
        
        return ret;
    }

    public boolean equalsIgnoreCase(Object obj)
    {
        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) 
        {
            return false;
        }
        Person rhs = (Person) obj;

//        boolean ret = new EqualsBuilder()
////                     .appendSuper(super.equals(obj))
//                     .append(getFirstNames(), rhs.getFirstNames())
//                     .append(getSurname(), rhs.getSurname())
//                     .isEquals();
//        
//        return ret;
        
        return (getFirstNames().equalsIgnoreCase(rhs.getFirstNames()) &&
                getSurname().equalsIgnoreCase(rhs.getSurname()));
    }
    
    @Override
    public String toString()
    {
        // TODO Auto-generated method stub
        return getFirstNames() + " " + getSurname();
    }

    @Id
    @GeneratedValue
    private long id;

    @Column(name = "surname", nullable = false)
    protected String surname;

    @Column(name = "first_names", nullable = false)
    protected String firstNames;

    @Column(name = "bday_18", nullable = true)
    protected Date birthday18;

    @ManyToOne
    @JoinColumn(name="first_seen_roll_id", nullable = true)
    protected ElectoralRoll firstSeenRoll;

    //??? why was this useful??
//    @OneToMany (cascade = CascadeType.ALL, mappedBy = "person")
//    @Sort (type=SortType.NATURAL)
//    Set<ElectoralRollEntry> electoralRollEntries = new TreeSet<ElectoralRollEntry>();
    
    @OneToMany (cascade = CascadeType.ALL, mappedBy = "person")
    @Sort (type=SortType.NATURAL)
    Set<VotingInfo> votingInfo = new TreeSet<VotingInfo>();
    
    @OneToMany (cascade = CascadeType.ALL, mappedBy = "person")
    @Sort (type=SortType.NATURAL)
    Set<Contact> contacts = new TreeSet<Contact>();
    
    public Set<VotingInfo> getVotingInfo()
    {
        return votingInfo;
    }

    public void addVotingInfo(VotingInfo vi)
    {
        vi.setPerson(this);
        votingInfo.add(vi);
    }

    public VotingInfo getLastVoted()
    {
        VotingInfo vi = null;
        
        if (getVotingInfo() != null)
        {
            // only interested in the most recent voting info
            Iterator<VotingInfo> it = getVotingInfo().iterator();            
        
            while (it.hasNext())
                vi = it.next();
        }
        return vi;
    }
    
    public Set<Contact> getContacts()
    {
        return contacts;
    }

    public void addContact(Contact c)
    {
        c.setPerson(this);
        contacts.add(c);
    }

    public Contact getLastContact()
    {
        Contact c = null;
        
        if (getContacts() != null)
        {
            // only interested in the most recent contact
            Iterator<Contact> it = getContacts().iterator();            
        
            while (it.hasNext())
                c = it.next();
        }
        return c;
    }
    
    public String getSurname()
    {
        return surname;
    }

//    public void addElectoralRollEntry (ElectoralRollEntry entry)
//    {
//        entry.setPerson(this);
//        electoralRollEntries.add(entry);
//    }
//    
//    
//    public Set<ElectoralRollEntry> getElectoralRollEntries()
//    {
//        return electoralRollEntries;
//    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getFirstNames()
    {
        return firstNames;
    }

    public void setFirstNames(String firstNames)
    {
        this.firstNames = firstNames;
    }

    public Date getBirthday18()
    {
        return birthday18;
    }

    public void setBirthday18(Date birthday18)
    {
        this.birthday18 = birthday18;
    }

    public long getId()
    {
        return id;
    }

    public ElectoralRoll getFirstSeenRoll()
    {
        return firstSeenRoll;
    }

    public void setFirstSeenRoll(ElectoralRoll firstSeenRoll)
    {
        this.firstSeenRoll = firstSeenRoll;
    }
    
}